package com.bistu.notepad_zjr.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.bistu.notepad_zjr.EditActivity;
import com.bistu.notepad_zjr.NoteDbOpenHelper;
import com.bistu.notepad_zjr.R;
import com.bistu.notepad_zjr.bean.Note;
import com.bistu.notepad_zjr.util.TimeChange;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Note> mBeanList;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private NoteDbOpenHelper mNoteDbOpenHelper;

    public MyAdapter(Context context, List<Note> mBeanList){
        this.mBeanList = mBeanList;
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mNoteDbOpenHelper = new NoteDbOpenHelper(mContext);
    }

    /**
     * 用于创建ViewHolder实例
     * 加载布局
     * @param parent
     * @param viewType 视图类型
     * @return
     */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {//ViewGroup parent, int viewType
        View view = mLayoutInflater.inflate(R.layout.list_item_grid_layout, parent, false);
        MyGridViewHolder myGridViewHolder = new MyGridViewHolder(view);
        return myGridViewHolder;
    }

    /**
     * 数据绑定
     * 对RecyclerView子项的数据进行赋值
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        bindGridViewHolder((MyGridViewHolder) holder, position);
    }

    /**
     * 返回数据源长度
     * @return
     */
    @Override
    public int getItemCount() {
        return mBeanList.size();
    }

    /**
     * 设置条目 数据
     * @param holder
     * @param position
     */
    private void bindGridViewHolder(MyGridViewHolder holder, final int position) {
        final Note note = mBeanList.get(position);

        holder.mTvTitle.setText(note.getTitle());
        holder.mAuthor.setText("作者："+ note.getAuthor());
        holder.mTvContent.setText(note.getContent());
        if (note.getEndTime()==0){
            holder.mTvTime.setText(note.getCreatedTime()+" ");
            holder.mTvEndTime.setText("无结束时间");
        } else {
            holder.mTvTime.setText(note.getCreatedTime()+"至");
            holder.mTvEndTime.setText(TimeChange.timeToStr(note.getEndTime()));
        }


        //点击条目时，跳转到编辑页面
        holder.rlContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, EditActivity.class);
                intent.putExtra("note", note);
                mContext.startActivity(intent);
            }
        });

        //长按时，弹出对话框
        holder.rlContainer.setOnLongClickListener(new View.OnLongClickListener() {
            AlertDialog.Builder dialog_zjr = new AlertDialog.Builder(mContext);
            @Override
            public boolean onLongClick(View v) {
                dialog_zjr.setMessage("更改或删除");
                dialog_zjr.setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //删除数据库中内容
                        int row = mNoteDbOpenHelper.deleteFromDbById(note.getId());
                        if (row > 0) {
                            //删除列表中内容
                            removeData(position);
                            notifyDataSetChanged();
                        }else
                            Toast.makeText(mContext, "删除失败！", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });

                dialog_zjr.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

                dialog_zjr.setNeutralButton("更改", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //跳转
                        Intent intent = new Intent(mContext, EditActivity.class);
                        intent.putExtra("note", note);
                        mContext.startActivity(intent);
                        dialog.dismiss();
                    }
                });
                dialog_zjr.create();
                dialog_zjr.show();
                return true;
            }
        });
    }

    //刷新数据
    public void refreshData(List<Note> notes) {
        this.mBeanList = notes;     //取到新数据
        notifyDataSetChanged();     //通知数据改变，实现刷新
    }

    //删除数据
    public void removeData(int pos) {
        mBeanList.remove(pos);
        notifyItemRemoved(pos);
    }

    class MyGridViewHolder extends RecyclerView.ViewHolder{
        TextView mTvTitle;      //标题
        TextView mTvContent;    //内容
        TextView mTvTime;       //时间
        TextView mTvEndTime;    //截至日期
        TextView mAuthor;
        ViewGroup rlContainer;  //布局

        public MyGridViewHolder(@NonNull View itemView) {
            super(itemView);
            this.mTvTitle = itemView.findViewById(R.id.tv_title);
            this.mTvContent = itemView.findViewById(R.id.tv_content);
            this.mTvTime = itemView.findViewById(R.id.tv_time);
            this.mTvEndTime = itemView.findViewById(R.id.tv_endTime);
            this.mAuthor = itemView.findViewById(R.id.tv_author);
            this.rlContainer = itemView.findViewById(R.id.rl_item_container);
        }
    }
}
